import http from './http'
import Page from '../utils/entity/Page'
import Result from '../utils/entity/Result'
import CodeExample from './entity/CodeExample'

const saveURL = '/code-example' // 新增
const updateURL = '/code-example/{encryptId}' // 修改
const deleteByIdURL = '/code-example/{encryptId}' // 按 encryptId 删除
const markDeleteByIdURL = '/code-example/{encryptId}' // 按 encryptId 逻辑删除
const markDeleteByIdsURL = '/code-example' // 按 encryptId 批量逻辑删除
const findByIdURL = '/code-example/{encryptId}' // 按 encryptId 查询单条记录
const listURL = '/code-example' // 多条件批量查询，不分页
const pageURL = '/code-example/page/{number}/{limit}' // 分页：多条件批量查询
const listVoURL = '/code-example/vo' // 多条件批量查询，不分页，投影到 VO 类
const pageVoURL = '/code-example/page/vo/{number}/{limit}' // 分页：多条件批量查询，投影到 VO 类

/**
 * 后台服务请求：参考案例：实体表操作
 * @author 谢长春 2019-7-28
 */
export default class CodeExampleService {
  /**
   * 新增
   * @param vo {CodeExample}
   * @return {Promise<Result>}
   */
  static async save(vo = new CodeExample()) {
    return await http
      .post(saveURL, vo.clearBlankValue())
      .then(Result.ofResponse)
      .catch(Result.ofCatch)
  }

  /**
   * 修改
   * @param vo {CodeExample}
   * @return {Promise<Result>}
   */
  static async update(vo = new CodeExample()) {
    const {
      id,
      encryptId,
      ...body
    } = vo
    return await http
      .put(updateURL.format(encryptId || 0), body)
      .then(Result.ofResponse)
      .catch(Result.ofCatch)
  }

  /**
   * 按 encryptId 删除
   * @param encryptId {string} {@link CodeExample#encryptId}
   * @return {Promise<Result>}
   */
  static async deleteById(encryptId) {
    return await http
      .delete(deleteByIdURL.format(encryptId || 0))
      .then(Result.ofResponse)
      .catch(Result.ofCatch)
  }

  /**
   * 按 encryptId 逻辑删除
   * @param encryptId {string} {@link CodeExample#encryptId}
   * @return {Promise<Result>}
   */
  static async markDeleteById(encryptId) {
    return await http
      .patch(markDeleteByIdURL.format(encryptId || 0))
      .then(Result.ofResponse)
      .catch(Result.ofCatch)
  }

  /**
   * 按 encryptId 批量逻辑删除
   * @param encryptIds {string[]} {@link CodeExample#encryptId}
   * @return {Promise<Result>}
   */
  static async markDeleteByIds(encryptIds) {
    return await http
      .patch(markDeleteByIdsURL, encryptIds)
      .then(Result.ofResponse)
      .catch(Result.ofCatch)
  }

  /**
   * 按 encryptId 查询单条记录
   * @param encryptId {string} {@link CodeExample#encryptId}
   * @return {Promise<Result>}
   */
  static async findById(encryptId) {
    /**
     * @type {Result<any>}
     */
    const result = await http
      .get(findByIdURL.format(encryptId || 0))
      .then(Result.ofResponse)
      .catch(Result.ofCatch)
    return result.dataTransform(row => new CodeExample(row))
  }

  /**
   * 多条件批量查询，不分页
   * @param vo {CodeExample}
   * @return {Promise<Result>}
   */
  static async list(vo = new CodeExample()) {
    /**
     *
     * @type {Result<any>}
     */
    const result = await http
      .get(listURL, {params: vo})
      .then(Result.ofResponse)
      .catch(Result.ofCatch)
    return result.dataTransform(row => new CodeExample(row))
  }

  /**
   * 分页：多条件批量查询
   * @param vo {CodeExample}
   * @return {Promise<Result>}
   */
  static async pageable(vo = new CodeExample()) {
    const {
      page,
      ...params
    } = vo
    /**
     *
     * @type {Result<any>}
     */
    const result = await http
      .get(pageURL.formatObject(page || Page.ofDefault()), {params})
      .then(Result.ofResponse)
      .catch(Result.ofCatch)
    return result.dataTransform(row => new CodeExample(row))
  }

  /**
   * 多条件批量查询，不分页
   * @param vo {CodeExample}
   * @return {Promise<Result>}
   */
  static async listVO(vo = new CodeExample()) {
    /**
     *
     * @type {Result<any>}
     */
    const result = await http
      .get(listVoURL, {params: vo})
      .then(Result.ofResponse)
      .catch(Result.ofCatch)
    return result.dataTransform(row => new CodeExample(row))
  }

  /**
   * 分页：多条件批量查询
   * @return {Promise<Result>}
   */
  static async pageableVO(vo = new CodeExample()) {
    const {
      page,
      ...params
    } = vo
    /**
     *
     * @type {Result<any>}
     */
    const result = await http
      .get(pageVoURL.formatObject(page || Page.ofDefault()), {params})
      .then(Result.ofResponse)
      .catch(Result.ofCatch)
    return result.dataTransform(row => new CodeExample(row))
  }
}
